package com.yljk.yljkproject.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.yilijk.base.constants.UserInfo;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.SharedPreferencesUtils;
import com.yljk.homedoctor.HomeUtils;
import com.yljk.live.common.utils.MediaCountyManager;
import com.yljk.servicemanager.constants.ReactApi;
import com.yljk.servicemanager.constants.RouterApi;
import com.yljk.yljkproject.MyApplication;
import com.yljk.yljkproject.ui.MainActivity;


/**
 * 推送消息的工具类
 */
public class PushMessageUtil {
    private static PushMessageUtil util = null;
    public static boolean imType = true;
    public static boolean homeType = true;

    private PushMessageUtil() {
    }

    public static PushMessageUtil getInstance() {
        if (util == null) {
            synchronized (PushMessageUtil.class) {
                if (util == null) {
                    util = new PushMessageUtil();
                }
            }
        }
        return util;
    }

    /**
     * 推送消息处理
     *
     * @param context
     * @param uriString
     * @param isLocal   是否本地消息列表点击
     * @return
     */
    public boolean dealMessage(Context context, String uriString, boolean isLocal) {
//        if (TextUtils.isEmpty(msg) || !msg.startsWith("com.audituser.scheme://")) {
//        if (TextUtils.isEmpty(msg) || !(msg.startsWith("com.audituser.scheme://") || msg.startsWith("com.auditdoctor.scheme://"))) {
        if (TextUtils.isEmpty(uriString)) {
            return false;
        }
        String token = SharedPreferencesUtils.getInstance().getString(UserInfo.token, null);
        String mobile = SharedPreferencesUtils.getInstance().getString(UserInfo.mobile, null);
        if (TextUtils.isEmpty(token) || TextUtils.isEmpty(mobile)) {
            return false;
        }
        String msg = uriString;
        if (msg.contains("com.doctor.scheme://") || msg.contains("com.patient.scheme://")) {
            msg = msg.replaceFirst("//", "");
        }
        msg = msg.replaceFirst(":", "://");
        try {
            msg = msg.replace("#", "%23");
            Uri uri = Uri.parse(msg);
            String path = uri.getLastPathSegment();
            String host = uri.getHost();
            String id = uri.getQueryParameter("id");
            ALog.e("注册：", "uri: " + uri + " " + path + " " + id);
            String switchStr = path;
            if (TextUtils.isEmpty(switchStr)) {
                switchStr = host;
            }
            switch (switchStr) {
                //首页
                case "jumpToHome": {
                    Intent intent = new Intent(context, MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("tag", "jumpToHome");
                    context.startActivity(intent);
                    return true;
                }
                //医生资质审核内容提交页面
                case "jumpToDoctoCertify": {
                    if (isLocal) {
                        Bundle bundle = new Bundle();
//                        bundle.putInt("notEdit", doctorDetailInfo.getPlatformAuditStatus());
                        bundle.putBoolean("azUseNew", true);
//                        bundle.putBoolean("azUseNew", doctorDetailInfo.isAzUseNew());
                        ReactUtils.INSTANCE.push(context, ReactApi.LOGIN_DOCTORBUNDLE, ReactApi.Login.QualificationPass, bundle);
                    } else {
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("tag", "jumpToDoctoCertify");
                        context.startActivity(intent);
                    }
                    return true;
                }
                //实名认证页面
                case "jumpToNameCertify": {
                    if (isLocal) {
                        ReactUtils.INSTANCE.push(context, ReactApi.LOGIN_DOCTORBUNDLE, ReactApi.Login.AutonymApprovePage);
                    } else {
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("tag", "jumpToNameCertify");
                        context.startActivity(intent);
                    }
                    return true;
                }
                //我的问诊页面
                case "jumpToMyInquiry": {
                    if (isLocal) {
                        ReactUtils.INSTANCE.push(context, ReactApi.LOGIN_DOCTORBUNDLE, ReactApi.Message.MyPhysicianVisits);
                    } else {
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("tag", "jumpToMyInquiry");
                        context.startActivity(intent);
                    }
                    return true;
                }
                //处方管理
                case "jumpToPrescriptions": {
                    if (isLocal) {
                        ReactUtils.INSTANCE.push(context, ReactApi.LOGIN_DOCTORBUNDLE, ReactApi.Message.Prescriptions);
                    } else {
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("tag", "jumpToPrescriptions");
                        context.startActivity(intent);
                    }
                    return true;
                }
                //我的钱包
                case "jumpToMoneyPackback": {
                    if (isLocal) {
                        ReactUtils.INSTANCE.push(context, ReactApi.LOGIN_DOCTORBUNDLE, ReactApi.Mine.MyWalletPage);
                    } else {
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("tag", "jumpToMoneyPackback");
                        context.startActivity(intent);
                    }
                    return true;
                }
                //客服（建议跳转修改银行卡信息页面）
                case "jumpToEditbankcard": {
                    if (isLocal) {
                        ReactUtils.INSTANCE.push(context, ReactApi.MY_DOCTORBUNDLE, ReactApi.Mine.Editbankcard);
                    } else {
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("tag", "jumpToMoneyPackback");
                        context.startActivity(intent);
                    }
                    return true;
                }
                //我的评价
                case "jumpToMyEvaluation": {
                    if (isLocal) {
                        ReactUtils.INSTANCE.push(context, ReactApi.LOGIN_DOCTORBUNDLE, ReactApi.Mine.MyEvaluationandWall);
                    } else {
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("tag", "jumpToMyEvaluation");
                        context.startActivity(intent);
                    }
                    return true;
                }
                case "url": {
                    String load = uri.getQueryParameter("load");
                    if (TextUtils.isEmpty(load)) {
                        return false;
                    }
                    if (isLocal) {
                        ARouter.getInstance().build(RouterApi.WebActivity)
                                .withString("url", load)
                                .withBoolean("ishtml", true)
                                .withBoolean("isShowLoading", true)
                                .navigation();
                    } else {
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("tag", "url");
                        intent.putExtra("urlLoad", load);
                        context.startActivity(intent);
                    }
                    return true;
                }
                case "jumpToLive": {
                    boolean isLivePush = MediaCountyManager.getInstance().pageClick(uriString);
                    return isLivePush;
                }
                case "general":{
                    Bundle bundle = new Bundle();
                    String compontName = "";
                    String pluginId = "";
                    // scheme分离获取入参string
                    String paramsString = uriString.replace("com.doctor.scheme://general?", "");
                    // 参数截取
                    // 通过参数连接符‘&’获取参数数组
                    String[] paramsArray = paramsString.split("&");
                    for (int i = 0; i < paramsArray.length; i++) {
                        //每个数组对象为 key=value
                        String paramsObject = paramsArray[i];
                        String paramsKey = paramsObject.substring(0,paramsObject.indexOf("=") );
                        String paramsValue = paramsObject.substring(paramsObject.indexOf("=")+1);
                        if (paramsKey.equals("module")){
                            pluginId = paramsValue;
                        }else if (paramsKey.equals("page")){
                            compontName = paramsValue;
                        }else{
                            bundle.putString(paramsKey,paramsValue);
                        }
                    }
                    switch (pluginId){
                        case "home":
                            pluginId = ReactApi.HOME_DOCTORBUNDLE;
                            break;
                        case "im":
                            pluginId = ReactApi.IM_DOCTORBUNDLE;
                            break;
                        case "login":
                            pluginId = ReactApi.LOGIN_DOCTORBUNDLE;
                            break;
                        case "my":
                            pluginId = ReactApi.MY_DOCTORBUNDLE;
                            break;
                        case "native":
                            nativeRouter(context,compontName,bundle);
                            return true;
                    }
                    ReactUtils.INSTANCE.push(context,pluginId,compontName,bundle);
                    return true;
                }
            }

            return false;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }

    private void nativeRouter(Context context, String compontName, Bundle bundle) {
        switch (compontName){
            case "FaceActivity":
                //人脸识别
                ARouter.getInstance()
                        .build(RouterApi.FaceActivity)
                        .withString("certifyId",bundle.getString("certifyId"))
                        .navigation();
                break;
            case "MessageList":
                //消息列表
                ARouter.getInstance()
                        .build(RouterApi.MAIN_ACTIVITY)
                        .withString("tag","jumpToMsg")
                        .navigation();
                break;
            case "CheckActivity":
                //合理用药检测
                ARouter.getInstance()
                        .build(RouterApi.CheckActivity)
                        .withString("sessionId",bundle.getString("sessionId"))
                        .withString("appParam",bundle.getString("appParam"))
                        .navigation();
                break;
            case "HomeShowView":
                //首页列表
                ARouter.getInstance()
                        .build(RouterApi.MAIN_ACTIVITY)
                        .withString("tag","jumpToHome")
                        .navigation();
                break;
            case "TaxAgreementView":
                //个税政策协议
                ARouter.getInstance()
                        .build(RouterApi.WebActivity)
                        .withBoolean("isPersonalTax", true)
                        .navigation();
                break;
            case "ScanQrCodeView":
                //扫码页面
                ARouter.getInstance()
                        .build(RouterApi.MAIN_ACTIVITY)
                        .withString("tag","jumpToQrCode")
                        .navigation();
                break;
            case "MineShowView":
                //我的页面

                break;
            case "SettingActivity":
                //设置页面

                break;
            case "WebViewActivity":
                //加载web页面

                break;
            case "VideoInfoActivity":
                //文章视频播放健康科普

                break;
            case "MediaPlayerActivity":
                //文章音频播放

                break;
        }
    }
}
